namespace	Elmah
{
	#region	using
	using	System;
	using	System.Web.Management;
	using	System.Collections.Specialized;
	using	System.Web;
	using	Elmah;
#endregion
		// Implements	a	event	provider for logging into	ELMAH	Database.
		public class ElmahEventProvider	:	BufferedWebEventProvider
		{
				private	string _providerName;
				private	ErrorLog _log;

				// Initializes the provider.
				public override	void Initialize(string name, NameValueCollection config)
				{
						base.Initialize(name,	config);
						_providerName	=	name;
						_log = Elmah.ErrorLog.Default;
				}

			
				// Processes the incoming	events.
				// This	method performs	custom processing	and, if
				// buffering is	enabled, it	calls	the	base.ProcessEvent
				// to	buffer the event information.
				public override	void ProcessEvent(WebBaseEvent eventRaised)
				{
						if (UseBuffering)
								// Buffering enabled,	call the base	event	to buffer	event	information.
								base.ProcessEvent(eventRaised);
						else
						{
								// Store the information in	the	specified	file.
								LogToElmah(eventRaised);
						}
				}

				// Processes the messages	that have	been buffered.
				// It	is called	by the ASP.NET when	the	flushing of	
				// the buffer	is required	according	to the parameters	
				// defined in	the	<bufferModes>	element	of the 
				// <healthMonitoring>	configuration	section.
				public override	void ProcessEventFlush(WebEventBufferFlushInfo flushInfo)
				{
						// Customize event information to	be logged.
						// flushInfo.NotificationType
						// flushInfo.EventsInBuffer
						// flushInfo.EventsDiscardedSinceLastNotification

						// Read	each buffered	event	and	send it	to the Log.
						foreach	(WebBaseEvent	eventRaised	in flushInfo.Events)
								LogToElmah(eventRaised);

				}

				// Performs	standard shutdown.
				public override	void Shutdown()
				{
						// Flush the buffer, if	needed.
						Flush();
				}

				// Store one event information in	ELMAH.
				private	void LogToElmah(WebBaseEvent healthEvent)
				{
						try
						{
								Elmah.HealthEventException exception = new Elmah.HealthEventException(healthEvent.ToString(true, true));
								Error	current	=	new	Error(exception, HttpContext.Current);
								current.ApplicationName	=	"HealthEvent";
								current.StatusCode = healthEvent.EventCode;
								current.Message	=	healthEvent.Message;
								_log.Log(current);

						}
						catch	(Exception e)
						{
								throw	new	Exception("ElmahEventProvider.LogToElmah:	"	+	e);
						}
				}

		}

}


